home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / termcap.c < prev    next >
C/C++ Source or Header  |  1993-10-07  |  16KB  |  728 lines

  1. /* Work-alike for termcap, plus extra features.
  2.    Copyright (C) 1985, 1986, 1993 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; see the file COPYING.  If not, write to
  16. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Emacs config.h may rename various library functions such as malloc.  */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21.  
  22. #ifdef emacs
  23. #include "lisp.h"
  24.  
  25. #ifdef STDC_HEADERS
  26. #include <stdlib.h>
  27. #include <io.h>
  28. #endif
  29. #include "termcap_p.h"
  30. static char * find_capability _P_((register char *bp,
  31.                                    register char *cap));
  32. static char * tgetst1 _P_((char *ptr, char **area));
  33. #endif
  34. #else /* not HAVE_CONFIG_H */
  35.  
  36. #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
  37. #define bcopy(s, d, n) memcpy ((d), (s), (n))
  38. #endif
  39.  
  40. #ifdef STDC_HEADERS
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #else
  44. char *getenv ();
  45. char *malloc ();
  46. char *realloc ();
  47. #endif
  48.  
  49. #ifdef HAVE_UNISTD_H
  50. #include <unistd.h>
  51. #endif
  52. #ifdef _POSIX_VERSION
  53. #include <fcntl.h>
  54. #endif
  55.  
  56. #endif /* not HAVE_CONFIG_H */
  57.  
  58. #ifndef NULL
  59. #define NULL (char *) 0
  60. #endif
  61.  
  62. /* BUFSIZE is the initial size allocated for the buffer
  63.    for reading the termcap file.
  64.    It is not a limit.
  65.    Make it large normally for speed.
  66.    Make it variable when debugging, so can exercise
  67.    increasing the space dynamically.  */
  68.  
  69. #ifndef BUFSIZE
  70. #ifdef DEBUG
  71. #define BUFSIZE bufsize
  72.  
  73. int bufsize = 128;
  74. #else
  75. #define BUFSIZE 2048
  76. #endif
  77. #endif
  78.  
  79. #ifndef emacs
  80. static void
  81. memory_out ()
  82. {
  83.   write (2, "virtual memory exhausted\n", 25);
  84.   exit (1);
  85. }
  86.  
  87. static char *
  88. xmalloc (size)
  89.      unsigned size;
  90. {
  91.   register char *tem = malloc (size);
  92.  
  93.   if (!tem)
  94.     memory_out ();
  95.   return tem;
  96. }
  97.  
  98. static char *
  99. xrealloc (ptr, size)
  100.      char *ptr;
  101.      unsigned size;
  102. {
  103.   register char *tem = realloc (ptr, size);
  104.  
  105.   if (!tem)
  106.     memory_out ();
  107.   return tem;
  108. }
  109. #endif /* not emacs */
  110.  
  111. /* Looking up capabilities in the entry already found.  */
  112.  
  113. /* The pointer to the data made by tgetent is left here
  114.    for tgetnum, tgetflag and tgetstr to find.  */
  115. static char *term_entry;
  116.  
  117. /* Search entry BP for capability CAP.
  118.    Return a pointer to the capability (in BP) if found,
  119.    0 if not found.  */
  120.  
  121. static char *
  122. find_capability (bp, cap)
  123.      register char *bp, *cap;
  124. {
  125.   for (; *bp; bp++)
  126.     if (bp[0] == ':'
  127.     && bp[1] == cap[0]
  128.     && bp[2] == cap[1])
  129.       return &bp[4];
  130.   return NULL;
  131. }
  132.  
  133. int
  134. tgetnum (cap)
  135.      char *cap;
  136. {
  137.   register char *ptr = find_capability (term_entry, cap);
  138.   if (!ptr || ptr[-1] != '#')
  139.     return -1;
  140.   return atoi (ptr);
  141. }
  142.  
  143. int
  144. tgetflag (cap)
  145.      char *cap;
  146. {
  147.   register char *ptr = find_capability (term_entry, cap);
  148.   return ptr && ptr[-1] == ':';
  149. }
  150.  
  151. /* Look up a string-valued capability CAP.
  152.    If AREA is non-null, it points to a pointer to a block in which
  153.    to store the string.  That pointer is advanced over the space used.
  154.    If AREA is null, space is allocated with `malloc'.  */
  155.  
  156. char *
  157. tgetstr (cap, area)
  158.      char *cap;
  159.      char **area;
  160. {
  161.   register char *ptr = find_capability (term_entry, cap);
  162.   if (!ptr || (ptr[-1] != '=' && ptr[-1] != '~'))
  163.     return NULL;
  164.   return tgetst1 (ptr, area);
  165. }
  166.  
  167. /* Table, indexed by a character in range 0100 to 0140 with 0100 subtracted,
  168.    gives meaning of character following \, or a space if no special meaning.
  169.    Eight characters per line within the string.  */
  170.  
  171. static char esctab[]
  172.   = " \007\010  \033\014 \
  173.       \012 \
  174.   \015 \011 \013 \
  175.         ";
  176.  
  177. /* PTR points to a string value inside a termcap entry.
  178.    Copy that value, processing \ and ^ abbreviations,
  179.    into the block that *AREA points to,
  180.    or to newly allocated storage if AREA is NULL.
  181.    Return the address to which we copied the value,
  182.    or NULL if PTR is NULL.  */
  183.  
  184. static char *
  185. tgetst1 (ptr, area)
  186.      char *ptr;
  187.      char **area;
  188. {
  189.   register char *p, *r;
  190.   register int c;
  191.   register int size;
  192.   char *ret;
  193.   register int c1;
  194.  
  195.   if (!ptr)
  196.     return NULL;
  197.  
  198.   /* `ret' gets address of where to store the string.  */
  199.   if (!area)
  200.     {
  201.       /* Compute size of block needed (may overestimate).  */
  202.       p = ptr;
  203.       while ((c = *p++) && c != ':' && c != '\n')
  204.     ;
  205.       ret = (char *) xmalloc (p - ptr + 1);
  206.     }
  207.   else
  208.     ret = *area;
  209.  
  210.   /* Copy the string value, stopping at null or colon.
  211.      Also process ^ and \ abbreviations.  */
  212.   p = ptr;
  213.   r = ret;
  214.   while ((c = *p++) && c != ':' && c != '\n')
  215.     {
  216.       if (c == '^')
  217.     c = *p++ & 037;
  218.       else if (c == '\\')
  219.     {
  220.       c = *p++;
  221.       if (c >= '0' && c <= '7')
  222.         {
  223.           c -= '0';
  224.           size = 0;
  225.  
  226.           while (++size < 3 && (c1 = *p) >= '0' && c1 <= '7')
  227.         {
  228.           c *= 8;
  229.           c += c1 - '0';
  230.           p++;
  231.         }
  232.         }
  233.       else if (c >= 0100 && c < 0200)
  234.         {
  235.           c1 = esctab[(c & ~040) - 0100];
  236.           if (c1 != ' ')
  237.         c = c1;
  238.         }
  239.     }
  240.       *r++ = c;
  241.     }
  242.   *r = '\0';
  243.   /* Update *AREA.  */
  244.   if (area)
  245.     *area = r + 1;
  246.   return ret;
  247. }
  248.  
  249. /* Outputting a string with padding.  */
  250.  
  251. short ospeed;
  252. /* If OSPEED is 0, we use this as the actual baud rate.  */
  253. int tputs_baud_rate;
  254. char PC;
  255.  
  256. /* Actual baud rate if positive;
  257.    - baud rate / 100 if negative.  */
  258.  
  259. static short speeds[] =
  260.   {
  261. #ifdef VMS
  262.     0, 50, 75, 110, 134, 150, -3, -6, -12, -18,
  263.     -20, -24, -36, -48, -72, -96, -192
  264. #else /* not VMS */
  265.     0, 50, 75, 110, 135, 150, -2, -3, -6, -12,
  266.     -18, -24, -48, -96, -192, -384
  267. #endif /* not VMS */
  268.   };
  269.  
  270. void
  271. tputs (str, nlines, outfun)
  272.      register char *str;
  273.      int nlines;
  274.      register void (*outfun) _P_((char));
  275. {
  276.   register int padcount = 0;
  277.   register int speed;
  278.  
  279. #ifdef emacs
  280.   extern baud_rate;
  281.   speed = baud_rate;
  282. #else
  283.   if (ospeed == 0)
  284.     speed = tputs_baud_rate;
  285.   else
  286.     speed = speeds[ospeed];
  287. #endif
  288.  
  289.   if (!str)
  290.     return;
  291.  
  292.   while (*str >= '0' && *str <= '9')
  293.     {
  294.       padcount += *str++ - '0';
  295.       padcount *= 10;
  296.     }
  297.   if (*str == '.')
  298.     {
  299.       str++;
  300.       padcount += *str++ - '0';
  301.     }
  302.   if (*str == '*')
  303.     {
  304.       str++;
  305.       padcount *= nlines;
  306.     }
  307.   while (*str)
  308.     (*outfun) (*str++);
  309.  
  310.   /* padcount is now in units of tenths of msec.  */
  311.   padcount *= speeds[ospeed];
  312.   padcount += 500;
  313.   padcount /= 1000;
  314.   if (speeds[ospeed] < 0)
  315.     padcount = -padcount;
  316.   else
  317.     {
  318.       padcount += 50;
  319.       padcount /= 100;
  320.     }
  321.  
  322.   while (padcount-- > 0)
  323.     (*outfun) (PC);
  324. }
  325.  
  326. /* Finding the termcap entry in the termcap data base.  */
  327.  
  328. struct buffer
  329.   {
  330.     char *beg;
  331.     int size;
  332.     char *ptr;
  333.     int ateof;
  334.     int full;
  335.   };
  336.  
  337. /* Forward declarations of static functions.  */
  338. static int scan_file _P_((char *str, int fd, register struct buffer *bufp));
  339. static int name_match _P_((char *line, char *name));
  340. static int compare_contin _P_((register char *str1, register char *str2));
  341. static char * gobble_line _P_((int fd, register struct buffer *bufp,
  342.                                char *append_end));
  343.  
  344. #ifdef VMS
  345.  
  346. #include <rmsdef.h>
  347. #include <fab.h>
  348. #include <nam.h>
  349.  
  350. static int
  351. valid_filename_p (fn)
  352.      char *fn;
  353. {
  354.   struct FAB fab = cc$rms_fab;
  355.   struct NAM nam = cc$rms_nam;
  356.   char esa[NAM$C_MAXRSS];
  357.  
  358.   fab.fab$l_fna = fn;
  359.   fab.fab$b_fns = strlen(fn);
  360.   fab.fab$l_nam = &nam;
  361.   fab.fab$l_fop = FAB$M_NAM;
  362.  
  363.   nam.nam$l_esa = esa;
  364.   nam.nam$b_ess = sizeof esa;
  365.  
  366.   return SYS$PARSE(&fab, 0, 0) == RMS$_NORMAL;
  367. }
  368.  
  369. #else /* !VMS */
  370.  
  371. #define valid_filename_p(fn) (*(fn) == '/')
  372.  
  373. #endif /* !VMS */
  374.  
  375. /* Find the termcap entry data for terminal type NAME
  376.    and store it in the block that BP points to.
  377.    Record its address for future use.
  378.  
  379.    If BP is null, space is dynamically allocated.
  380.  
  381.    Return -1 if there is some difficulty accessing the data base
  382.    of terminal types,
  383.    0 if the data base is accessible but the type NAME is not defined
  384.    in it, and some other value otherwise.  */
  385.  
  386. int
  387. tgetent (bp, name)
  388.      char *bp, *name;
  389. {
  390.   register char *termcap_name;
  391.   register int fd;
  392.   struct buffer buf;
  393.   register char *bp1;
  394.   char *bp2;
  395.   char *term;
  396.   int malloc_size = 0;
  397.   register int c;
  398.   char *tcenv;            /* TERMCAP value, if it contains :tc=.  */
  399.   char *indirect = NULL;    /* Terminal type in :tc= in TERMCAP value.  */
  400.   int filep;
  401.  
  402.   termcap_name = getenv ("TERMCAP");
  403.   if (termcap_name && *termcap_name == '\0')
  404.     termcap_name = NULL;
  405.  
  406.   filep = termcap_name && valid_filename_p (termcap_name);
  407.  
  408.   /* If termcap_name is non-null and starts with / (in the un*x case, that is),
  409.      it is a file name to use instead of /etc/termcap.
  410.      If it is non-null and does not start with /,
  411.      it is the entry itself, but only if
  412.      the name the caller requested matches the TERM variable.  */
  413.  
  414.   if (termcap_name && !filep && !strcmp (name, getenv ("TERM")))
  415.     {
  416.       indirect = tgetst1 (find_capability (termcap_name, "tc"), (char **) 0);
  417.       if (!indirect)
  418.     {
  419.       if (!bp)
  420.         bp = termcap_name;
  421.       else
  422.         strcpy (bp, termcap_name);
  423.       goto ret;
  424.     }
  425.       else
  426.     {            /* It has tc=.  Need to read /etc/termcap.  */
  427.       tcenv = termcap_name;
  428.        termcap_name = NULL;
  429.     }
  430.     }
  431.  
  432.   if (!termcap_name || !filep)
  433. #ifdef VMS
  434.     termcap_name = "emacs_library:[etc]termcap.dat";
  435. #else
  436.     termcap_name = "/etc/termcap";
  437. #endif
  438.  
  439.   /* Here we know we must search a file and termcap_name has its name.  */
  440.  
  441.   fd = open (termcap_name, 0, 0);
  442.   if (fd < 0)
  443.     return -1;
  444.  
  445.   buf.size = BUFSIZE;
  446.   /* Add 1 to size to ensure room for terminating null.  */
  447.   buf.beg = (char *) xmalloc (buf.size + 1);
  448.   term = indirect ? indirect : name;
  449.  
  450.   if (!bp)
  451.     {
  452.       malloc_size = indirect ? strlen (tcenv) + 1 : buf.size;
  453.       bp = (char *) xmalloc (malloc_size);
  454.     }
  455.   bp1 = bp;
  456.  
  457.   if (indirect)
  458.     /* Copy the data from the environment variable.  */
  459.     {
  460.       strcpy (bp, tcenv);
  461.       bp1 += strlen (tcenv);
  462.     }
  463.  
  464.   while (term)
  465.     {
  466.       /* Scan the file, reading it via buf, till find start of main entry.  */
  467.       if (scan_file (term, fd, &buf) == 0)
  468.     {
  469.       close (fd);
  470.       free (buf.beg);
  471.       if (malloc_size)
  472.         free (bp);
  473.       return 0;
  474.     }
  475.  
  476.       /* Free old `term' if appropriate.  */
  477.       if (term != name)
  478.     free (term);
  479.  
  480.       /* If BP is malloc'd by us, make sure it is big enough.  */
  481.       if (malloc_size)
  482.     {
  483.       malloc_size = bp1 - bp + buf.size;
  484.       termcap_name = (char *) xrealloc (bp, malloc_size);
  485.       bp1 += termcap_name - bp;
  486.       bp = termcap_name;
  487.     }
  488.  
  489.       bp2 = bp1;
  490.  
  491.       /* Copy the line of the entry from buf into bp.  */
  492.       termcap_name = buf.ptr;
  493.       while ((*bp1++ = c = *termcap_name++) && c != '\n')
  494.     /* Drop out any \ newline sequence.  */
  495.     if (c == '\\' && *termcap_name == '\n')
  496.       {
  497.         bp1--;
  498.         termcap_name++;
  499.       }
  500.       *bp1 = '\0';
  501.  
  502.       /* Does this entry refer to another terminal type's entry?
  503.      If something is found, copy it into heap and null-terminate it.  */
  504.       term = tgetst1 (find_capability (bp2, "tc"), (char **) 0);
  505.     }
  506.  
  507.   close (fd);
  508.   free (buf.beg);
  509.  
  510.   if (malloc_size)
  511.     bp = (char *) xrealloc (bp, bp1 - bp + 1);
  512.  
  513.  ret:
  514.   term_entry = bp;
  515.   if (malloc_size)
  516.     return (int) bp;
  517.   return 1;
  518. }
  519.  
  520. /* Given file open on FD and buffer BUFP,
  521.    scan the file from the beginning until a line is found
  522.    that starts the entry for terminal type STR.
  523.    Return 1 if successful, with that line in BUFP,
  524.    or 0 if no entry is found in the file.  */
  525.  
  526. static int
  527. scan_file (str, fd, bufp)
  528.      char *str;
  529.      int fd;
  530.      register struct buffer *bufp;
  531. {
  532.   register char *end;
  533.  
  534.   bufp->ptr = bufp->beg;
  535.   bufp->full = 0;
  536.   bufp->ateof = 0;
  537.   *bufp->ptr = '\0';
  538.  
  539.   lseek (fd, 0L, 0);
  540.  
  541.   while (!bufp->ateof)
  542.     {
  543.       /* Read a line into the buffer.  */
  544.       end = NULL;
  545.       do
  546.     {
  547.       /* if it is continued, append another line to it,
  548.          until a non-continued line ends.  */
  549.       end = gobble_line (fd, bufp, end);
  550.     }
  551.       while (!bufp->ateof && end[-2] == '\\');
  552.  
  553.       if (*bufp->ptr != '#'
  554.       && name_match (bufp->ptr, str))
  555.     return 1;
  556.  
  557.       /* Discard the line just processed.  */
  558.       bufp->ptr = end;
  559.     }
  560.   return 0;
  561. }
  562.  
  563. /* Return nonzero if NAME is one of the names specified
  564.    by termcap entry LINE.  */
  565.  
  566. static int
  567. name_match (line, name)
  568.      char *line, *name;
  569. {
  570.   register char *tem;
  571.  
  572.   if (!compare_contin (line, name))
  573.     return 1;
  574.   /* This line starts an entry.  Is it the right one?  */
  575.   for (tem = line; *tem && *tem != '\n' && *tem != ':'; tem++)
  576.     if (*tem == '|' && !compare_contin (tem + 1, name))
  577.       return 1;
  578.  
  579.   return 0;
  580. }
  581.  
  582. static int
  583. compare_contin (str1, str2)
  584.      register char *str1, *str2;
  585. {
  586.   register int c1, c2;
  587.   while (1)
  588.     {
  589.       c1 = *str1++;
  590.       c2 = *str2++;
  591.       while (c1 == '\\' && *str1 == '\n')
  592.     {
  593.       str1++;
  594.       while ((c1 = *str1++) == ' ' || c1 == '\t');
  595.     }
  596.       if (c2 == '\0')
  597.     {
  598.       /* End of type being looked up.  */
  599.       if (c1 == '|' || c1 == ':')
  600.         /* If end of name in data base, we win.  */
  601.         return 0;
  602.       else
  603.         return 1;
  604.         }
  605.       else if (c1 != c2)
  606.     return 1;
  607.     }
  608. }
  609.  
  610. /* Make sure that the buffer <- BUFP contains a full line
  611.    of the file open on FD, starting at the place BUFP->ptr
  612.    points to.  Can read more of the file, discard stuff before
  613.    BUFP->ptr, or make the buffer bigger.
  614.  
  615.    Return the pointer to after the newline ending the line,
  616.    or to the end of the file, if there is no newline to end it.
  617.  
  618.    Can also merge on continuation lines.  If APPEND_END is
  619.    non-null, it points past the newline of a line that is
  620.    continued; we add another line onto it and regard the whole
  621.    thing as one line.  The caller decides when a line is continued.  */
  622.  
  623. static char *
  624. gobble_line (fd, bufp, append_end)
  625.      int fd;
  626.      register struct buffer *bufp;
  627.      char *append_end;
  628. {
  629.   register char *end;
  630.   register int nread;
  631.   register char *buf = bufp->beg;
  632.   register char *tem;
  633.  
  634.   if (!append_end)
  635.     append_end = bufp->ptr;
  636.  
  637.   while (1)
  638.     {
  639.       end = append_end;
  640.       while (*end && *end != '\n') end++;
  641.       if (*end)
  642.         break;
  643.       if (bufp->ateof)
  644.     return buf + bufp->full;
  645.       if (bufp->ptr == buf)
  646.     {
  647.       if (bufp->full == bufp->size)
  648.         {
  649.           bufp->size *= 2;
  650.           /* Add 1 to size to ensure room for terminating null.  */
  651.           tem = (char *) xrealloc (buf, bufp->size + 1);
  652.           bufp->ptr = (bufp->ptr - buf) + tem;
  653.           append_end = (append_end - buf) + tem;
  654.           bufp->beg = buf = tem;
  655.         }
  656.     }
  657.       else
  658.     {
  659.       append_end -= bufp->ptr - buf;
  660.       bcopy (bufp->ptr, buf, bufp->full -= bufp->ptr - buf);
  661.       bufp->ptr = buf;
  662.     }
  663.       if (!(nread = read (fd, buf + bufp->full, bufp->size - bufp->full)))
  664.     bufp->ateof = 1;
  665.       bufp->full += nread;
  666.       buf[bufp->full] = '\0';
  667.     }
  668.   return end + 1;
  669. }
  670.  
  671. #ifdef TEST
  672.  
  673. #ifdef NULL
  674. #undef NULL
  675. #endif
  676.  
  677. #include <stdio.h>
  678.  
  679. main (argc, argv)
  680.      int argc;
  681.      char **argv;
  682. {
  683.   char *term;
  684.   char *buf;
  685.  
  686.   term = argv[1];
  687.   printf ("TERM: %s\n", term);
  688.  
  689.   buf = (char *) tgetent (0, term);
  690.   if ((int) buf <= 0)
  691.     {
  692.       printf ("No entry.\n");
  693.       return 0;
  694.     }
  695.  
  696.   printf ("Entry: %s\n", buf);
  697.  
  698.   tprint ("cm");
  699.   tprint ("AL");
  700.  
  701.   printf ("co: %d\n", tgetnum ("co"));
  702.   printf ("am: %d\n", tgetflag ("am"));
  703. }
  704.  
  705. tprint (cap)
  706.      char *cap;
  707. {
  708.   char *x = tgetstr (cap, 0);
  709.   register char *y;
  710.  
  711.   printf ("%s: ", cap);
  712.   if (x)
  713.     {
  714.       for (y = x; *y; y++)
  715.     if (*y <= ' ' || *y == 0177)
  716.       printf ("\\%0o", *y);
  717.     else
  718.       putchar (*y);
  719.       free (x);
  720.     }
  721.   else
  722.     printf ("none");
  723.   putchar ('\n');
  724. }
  725.  
  726. #endif /* TEST */
  727.  
  728.